package com.hongyuan.sellfitness.ui.person.fragment;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomFragment;
import com.hongyuan.sellfitness.databinding.FragmentMyGroupBinding;
import com.hongyuan.sellfitness.ui.main.activity.ScanActivity;
import com.hongyuan.sellfitness.ui.person.view_mode.MyGroupViewModel;
import com.hongyuan.sellfitness.utils.CustomDialog;
import com.hongyuan.sellfitness.utils.TimeUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import me.tatarka.bindingcollectionadapter2.BR;

public class MyGroupFragment extends CustomFragment<FragmentMyGroupBinding, MyGroupViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_group;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MyGroupViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MyGroupViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X3);
        setEnableLoadMore(Constants.GET_MEMBER_SIGN_UP_COURSE_SUPER_LIST);
        setEnableRefresh(Constants.GET_MEMBER_SIGN_UP_COURSE_SUPER_LIST);
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams();
        viewModel.clearParams();
        //.setParams("os_id",viewModel.userToken.getOs_id());
        viewModel.requestData(Constants.GET_MEMBER_SIGN_UP_COURSE_SUPER_LIST);
    }

    @SuppressLint("CheckResult")
    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showSuccess.observe(this, aVoid -> {
            CustomDialog.groupCoursePunchSuccess(getContext(), TimeUtil.formatDataMsec(TimeUtil.dateFormatDotMD,System.currentTimeMillis()),
                    TimeUtil.getWeek());
        });
        viewModel.uc.goScan.observe(this,aVoid -> {
            //摄像权限权限请求
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(Manifest.permission.CAMERA)
                    .subscribe(aBoolean -> {
                        if (aBoolean) {
                            startActivity(ScanActivity.class);
                        } else {
                            ToastUtils.showShort("权限被拒绝");
                        }
                    });
        });
        viewModel.uc.cancel.observe(this,aVoid -> {
            CustomDialog.promptDialog(getContext(), "确定取消报名该团课吗？", "确定取消", "暂不取消", false, v1 -> {
                if(v1.getId() == R.id.isOk){
                    if(viewModel.itemViewModel != null){
                        //调用取消预约接口
                        viewModel.setParams("ocs_id",String.valueOf(viewModel.itemViewModel.entity.get().getOcs_id()));
                        viewModel.requestData(Constants.CANCEL_COURSE_SUPER_ORDER);
                    }
                }
            });
        });
    }
}
